package com.mxpio.erp.technology.controller;

import com.mxpio.erp.common.technology.entity.ProdTecRoutProcTmp;
import com.mxpio.erp.common.technology.service.ProdTecRoutProcTmpService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProdTecRoutProcTmpController", description = "产品工艺路线草稿行接口")
@RestController
@RequestMapping("/erp/tech/prodrout/proc/tmp/")
public class ProdTecRoutProcTmpController {

    @Autowired
    private ProdTecRoutProcTmpService routProcTmpService;

    @GetMapping("list")
    @Operation(summary = "产品工艺路线草稿行列表", description = "获取产品工艺路线草稿行列表（无分页）", method = "GET")
    public Result<List<ProdTecRoutProcTmp>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProdTecRoutProcTmp> bomInfoLineList = routProcTmpService.list(ProdTecRoutProcTmp.class, criteria);
        return Result.OK(bomInfoLineList);
    }

    @GetMapping("page")
    @Operation(summary = "产品工艺路线草稿行列表", description = "获取产品工艺路线草稿行列表（分页）", method = "GET")
    public Result<Page<ProdTecRoutProcTmp>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProdTecRoutProcTmp> bomInfoLineList = routProcTmpService.listPage(ProdTecRoutProcTmp.class, page, criteria);
        return Result.OK(bomInfoLineList);
    }
    @GetMapping("list/{routId}")
    @Operation(summary = "根据routId获取产品工艺路线草稿行", description = "根据routId获取产品工艺路线草稿行", method = "GET")
    public Result<List<ProdTecRoutProcTmp>> getByBomId(@Parameter(description="routId") @PathVariable(name = "routId", required = true) String routId) {
        Criteria c = Criteria.create().addCriterion("routId", Operator.EQ, routId);
        List<ProdTecRoutProcTmp> bomInfoLineList = routProcTmpService.list(ProdTecRoutProcTmp.class, c);
        return Result.OK(bomInfoLineList);
    }

    /*@GetMapping("list/{bomLineId}")
    @Operation(summary = "根据bomLineId获取产品工艺路线草稿行", description = "根据bomLineId获取产品工艺路线草稿行", method = "GET")
    public Result<ProdTecRoutProcTmp> getById(@Parameter(description="bomLineId") @PathVariable(name = "bomLineId", required = true)String bomLineId) {
        ProdTecRoutProcTmp bomInfoLine = routProcTmpService.getById(ProdTecRoutProcTmp.class, bomLineId);
        return Result.OK(bomInfoLine);
    }

    @PostMapping("add")
    @Operation(summary = "新增产品工艺路线草稿行", description = "新增产品工艺路线草稿行", method = "POST")
    public Result<ProdTecRoutProcTmp> add(@RequestBody ProdTecRoutProcTmp bomInfoLine) {
        routProcTmpService.save(bomInfoLine);
        return Result.OK(bomInfoLine);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑产品工艺路线草稿行", description = "编辑产品工艺路线草稿行", method = "PUT")
    public Result<ProdTecRoutProcTmp> edit(@RequestBody ProdTecRoutProcTmp bomInfoLine) {
        routProcTmpService.update(bomInfoLine);
        return Result.OK(bomInfoLine);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除产品工艺路线草稿行", description = "删除产品工艺路线草稿行,多个id用英文逗号分隔", method = "DELETE")
    public Result<ProdTecRoutProcTmp> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            routProcTmpService.remove(ProdTecRoutProcTmp.class, key);
        }
        return Result.OK();
    }*/
}
